﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="GetDeployerLogsCmdlet.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace CloudBlitz.Powershell.Cmdlets
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Management.Automation;

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Auth;
    using Microsoft.WindowsAzure.Storage.Table;

    [Cmdlet(VerbsCommon.Get, "DeployerLogs")]
    public class GetDeployerLogsCmdlet : PSCmdlet
    {
        [Parameter(Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string StorageAccountName { get; set; }

        [Parameter(Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string StorageAccountKey { get; set; }

        [Parameter(Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string DeploymentId { get; set; }

        [Parameter]
        public long Since { get; set; }

        [Parameter]
        public SwitchParameter Https { get; set; }

        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            try
            {
                base.ProcessRecord();
                this.GetDeployerLogsProcess();
            }
            catch (Exception ex)
            {
                var errorRecord = new ErrorRecord(ex, string.Empty, ErrorCategory.NotSpecified, this)
                {
                    ErrorDetails = new ErrorDetails(ex.Message)
                };

                this.ThrowTerminatingError(errorRecord);
            }
        }

        private void GetDeployerLogsProcess()
        {
            var storageAccountCredentials = new StorageCredentials(this.StorageAccountName, this.StorageAccountKey);

            var storageAccount = new CloudStorageAccount(storageAccountCredentials, this.Https.IsPresent);

            var cloudTableClient = storageAccount.CreateCloudTableClient();

            var wadLogsTable = cloudTableClient.GetTableReference("WADLogsTable");

            if (wadLogsTable.Exists())
            {
                var deploymentIdCondition = TableQuery.GenerateFilterCondition("DeploymentId", QueryComparisons.Equal, this.DeploymentId);
                var filter = deploymentIdCondition;
                if (this.Since > 0)
                {
                    var eventTickCountCondition = TableQuery.GenerateFilterConditionForLong("EventTickCount", QueryComparisons.GreaterThan, this.Since);
                    filter = TableQuery.CombineFilters(filter, TableOperators.And, eventTickCountCondition);
                }

                var tableQuery = new TableQuery<WADLogsTableEntity>().Where(filter);

                this.WriteObject(wadLogsTable.ExecuteQuery(tableQuery).OrderBy(x => x.EventTickCount).ToList(), true);
            }

            this.WriteObject(new List<WADLogsTableEntity>());
        }
    }
}